package kr.haggenna.member.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kr.haggenna.member.dao.MemberDao;
import kr.haggenna.member.domain.MemberCommand;
import kr.haggenna.member.util.PagingUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MemberListController {
	
	@Autowired
	private MemberDao memberDao;
	
	private int pageSize = 10;
	private int blockCount = 10;
	
	@RequestMapping("/memberList.do")
	public ModelAndView process(@RequestParam(value="pageNum",defaultValue="1")int currentPage,
								@RequestParam(value="keyField",defaultValue="")String keyField,
								@RequestParam(value="keyWord",defaultValue="")String keyWord){
		String pagingHtml = "";
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("keyField", keyField);
		map.put("keyWord", keyWord);
		
		int count = memberDao.getRowCount(map);
		
		PagingUtil page = new PagingUtil(keyField, keyWord, currentPage, count, pageSize,blockCount,"memberList.do");
		
		pagingHtml = page.getPagingHtml().toString();
		
		map.put("start", page.getStartCount());
		map.put("end", page.getEndCount());
		
		List<MemberCommand> memberList = null;
		if(count > 0 ){
			memberList = memberDao.list(map);
		}else{
			memberList = Collections.emptyList();
		}
		
		int size = memberList.size();
		int number = count-(currentPage-1)*pageSize;
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("memberList");
		mav.addObject("count",count);
		mav.addObject("size",size);
		mav.addObject("memberList", memberList);
		mav.addObject("pagingHtml", pagingHtml);
		mav.addObject("number", number);
		return mav;
	}
}
